package com.runvp.common.pay.wechat;

import com.runvp.common.exception.base.BaseException;
import org.apache.commons.codec.binary.Base64;

import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;

public class SignUtil {

    private static final String ENCODING = "UTF-8";
    private static final String SIGNATURE_ALGORITHM = "SHA256withRSA";


    /**
     * SHA256WithRSA签名
     *
     * @param data
     * @param privateKey
     * @return String
     */
    public static String sign256(String data, PrivateKey privateKey){
        try{
            Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
            signature.initSign(privateKey);
            signature.update(data.getBytes(ENCODING));
            return encodeBase64(signature.sign());
        }catch (Exception e){
            throw new BaseException("签名失败");
        }
    }
    /**
     * SHA256WithRSA签名
     *
     * @param data
     * @param privateKey
     * @return byte[]
     */
    public static byte[] sign256Byte(String data, PrivateKey privateKey){
        try{
            Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
            signature.initSign(privateKey);
            signature.update(data.getBytes(ENCODING));
            return signature.sign();
        }catch (Exception e){
            throw new BaseException("签名失败");
        }
    }

    public static boolean verify256(String data, byte[] sign, PublicKey publicKey) {
        if (data == null || sign == null || publicKey == null) {
            return false;
        }

        try {
            Signature signetcheck = Signature.getInstance(SIGNATURE_ALGORITHM);
            signetcheck.initVerify(publicKey);
            signetcheck.update(data.getBytes("UTF-8"));
            return signetcheck.verify(sign);
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 二进制数据编码为BASE64字符串
     *
     * @param bytes
     * @return String
     */
    public static String encodeBase64(byte[] bytes) {
        return new String(Base64.encodeBase64(bytes));
    }

    /**
     * BASE64解码
     *
     * @param bytes
     * @return
     */
    public static byte[] decodeBase64(byte[] bytes) {
        byte[] result = null;
        try {
            result = Base64.decodeBase64(bytes);
        } catch (Exception e) {
            return null;
        }
        return result;
    }
}